package j2024.j202409;

import java.util.*;

/**
 * 5 5
 * 1 1 5 5
 * .....
 * ****.
 * .....
 * **.**
 * .....
 */
public class j0925 {
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int[] dp = new int[m+1];
        dp[1] = n;
        for (int i = 2; i <= m; i++) {
            dp[i] = (dp[i-1]*(n-1))%109;
        }
        System.out.println(dp[m]);
    }
    public static int x1,y1,x2,y2,n,m,ret;
    public static int[] dx = {0,0,-1,1};
    public static int[] dy = {1,-1,0,0};
    public static int[][] vis;
    public static char[][] path;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();m = in.nextInt();
        x1 = in.nextInt()-1;y1 = in.nextInt()-1;
        x2 = in.nextInt()-1;y2 = in.nextInt()-1;
        vis = new int[n][m];
        path = new char[n][m];
        for (int i = 0; i < n; i++) {
            path[i] = in.next().toCharArray();
        }
        if(path[x2][y2]=='*'){
            System.out.println(-1);
        }
        System.out.println(bfs());
    }
    public static int  bfs(){
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                vis[i][j] = -1;
            }
        }
        Queue<int[]> queue = new LinkedList<>();
        queue.add(new int[]{x1,y1});
        vis[x1][y1] = 0;
        while(!queue.isEmpty()){
            int[] t = queue.poll();
            int a = t[0],b = t[1];
            for (int i = 0; i < 4; i++) {
                int x = a+dx[i],y = b+dy[i];
                if(x>=0 && x<n && y>=0 && y<m && path[x][y]=='.' && vis[x][y]==-1 ){
                        queue.add(new int[]{x,y});
                        vis[x][y] = vis[a][b]+1;
                        if(x==x2 && y==y2){
                            return vis[x][y];
                        }
                }
            }
        }
        return -1;
    }
}
class www{
    public int minmumNumberOfHost (int n, int[][] startEnd) {
        // write code here
        Arrays.sort(startEnd,(v1,v2)->{
            return v1[0]-v2[0];
        });
        PriorityQueue<Integer> queue = new PriorityQueue<>();
        queue.offer(startEnd[0][1]);
        for (int i = 1; i < n; i++) {
            int tmp = queue.peek();
            if(startEnd[i][0]<tmp){
                queue.offer(startEnd[i][0]);
            }else {
                queue.poll();
                queue.offer(startEnd[i][1]);
            }
        }

        return queue.size();
    }

    /**
     * 36. 有效的数独
     * 请你判断一个 9 x 9 的数独是否有效。只需要 根据以下规则 ，
     * 验证已经填入的数字是否有效即可。
     *
     * 数字 1-9 在每一行只能出现一次。
     * 数字 1-9 在每一列只能出现一次。
     * 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。（请参考示例图）
     */
    boolean[][] col,raw;
    boolean[][][] grid;
    public boolean isValidSudoku(char[][] board) {
        col = new boolean[9][10];
        raw = new boolean[9][10];
        grid = new boolean[3][3][9];
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if(board[i][j]!='.'){
                    int tmp = board[i][j]-'0';
                    if(col[i][tmp]||raw[i][tmp]||grid[i/3][j/3][tmp]){
                        return false;
                    }
                    col[i][tmp]=raw[i][tmp]=grid[i/3][j/3][tmp]=true;
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n];
        int sum = 0;
        for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
            sum+=arr[i];
        }
        if(sum%2==1){
            System.out.println("false");
        }else {
            sum/=2;
            boolean[][] dp = new boolean[n+1][sum+1];
            dp[0][0] = true;
            for (int i = 1; i <= n; i++) {
                for (int j = 0; j <= sum; j++) {
                    dp[i][j] = dp[i-1][j];
                    if(j>=arr[i]){
                        dp[i][j] = dp[i-1][j]||dp[i-1][j-arr[i]];
                    }
                }
            }
            if(dp[n][sum]){
                System.out.println("True");
            }
        }
    }
}
